<template>
	<el-dialog v-model="dialogVisible" append-to-body :show-close="false" :close-on-click-modal="false"
		class="flex-align" style="width: 100%;height: 100%;padding: 30px 24px;margin: 0;background: transparent;">
		<template #header>
			<el-card style="width: 100%;height: 100%;" body-style="padding: 0;" shadow="never">
				<template #header>
					<div class="flex-space-between">
						<div>
							<el-text style="font-size: 16px;">
								采购订单
							</el-text>
						</div>
						<div>
							<el-button @click="closeDialog">取消</el-button>
							<!-- <el-button @click="closeDialog">
								保存并继续创建
								<el-icon style="margin-left: 4px;">
									<ArrowDown />
								</el-icon>
							</el-button> -->
							<el-button type="primary" @click="submitForm(vFormRef)">
								保存
							</el-button>
						</div>
					</div>
				</template>
				<el-scrollbar ref="scrollbarRef" @scroll="handleScroll"
					:style="`height: calc(100vh - 69px - ( 30px + 30px));`">
					<el-form :model="formData" :rules="rules" ref="vFormRef" label-position="top" label-width="50px"
						style="padding: 16px;">
						<el-row :gutter="16">
							<el-col :span="12">
								<el-form-item class="form-item-bg" label="采购状态" prop="procurementStatus">
									<el-select @clear="selectClear('procurementStatus')"
										v-model="formData.procurementStatus"
										@change="(value:any)=>selectChange(value,'procurementStatus')" multiple
										ref="procurementStatus">
										<el-option v-for="item in procurementStatusOption" :key="key"
											:label="item?.label" :value="item?.value" disabled>
											<el-tag style="border: 0;" :color="item?.color" round effect="dark">
												{{ item?.label }}
											</el-tag>
										</el-option>
										<!--  -->
										<template #tag>
											<el-tag style="border: 0;"
												v-show="procurementStatusOption[procurementStatusIndex]?.value"
												:color="procurementStatusOption[procurementStatusIndex]?.color" round
												effect="dark">
												{{ procurementStatusOption[procurementStatusIndex]?.label }}
											</el-tag>
										</template>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item class="form-item-bg" label="采购单号" prop="purchaseOrderNumber">
									<el-input v-model="formData.purchaseOrderNumber" readonly
										placeholder="自动生成无需填写"></el-input>
								</el-form-item>
							</el-col>
						</el-row>
						<div class="static-content-item">
							<div>采购信息</div>
						</div>
						<el-form-item label="供应商" prop="supplier">
							<el-select @clear="selectClear('supplier')" v-model="formData.supplier"
								@change="(value:any)=>selectChange(value,'supplier')" multiple ref="supplier" clearable>
								<el-option v-for="item in supplierOption" :key="key" :label="item?.label"
									:value="item?.value">
									<!--  -->
									<div class="flex-align-center">
										<el-icon :color="item?.color">
											<component :is="item?.icon"></component>
										</el-icon>
										<!--  -->
										<el-text style="margin-left: 6px;">{{item?.label}}</el-text>
									</div>
								</el-option>
								<!--  -->
								<template #tag>
									<div class="flex-align-center">
										<el-icon :color="supplierOption[supplierIndex]?.color">
											<component :is="supplierOption[supplierIndex]?.icon">
											</component>
										</el-icon>
										<!--  -->
										<el-text size="small"
											style="margin-left: 6px;">{{supplierOption[supplierIndex]?.label}}</el-text>
									</div>
								</template>
							</el-select>
						</el-form-item>
						<el-form-item label="预计到货日期" prop="expectedArrivalDate">
							<el-date-picker v-model="formData.expectedArrivalDate" style="width:100%;" type="date" />
						</el-form-item>
						<el-form-item label="备注" prop="remark">
							<el-input v-model="formData.remark" placeholder="备注"></el-input>
						</el-form-item>
						<el-row :gutter="16">
							<el-col :span="12">
								<el-form-item label="采购申请人" prop="purchaseApplicant">
									<el-input v-model="formData.purchaseApplicant" readonly
										placeholder="采购申请人"></el-input>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="采购申请日期" prop="purchaseApplicationDate">
									<el-date-picker v-model="formData.purchaseApplicationDate" style="width:100%;"
										type="date" />
								</el-form-item>
							</el-col>
						</el-row>
						<!--  -->
						<div>
							<div class="static-content-item">
								<div>采购入库</div>
							</div>
							<el-form-item label="采购总数" prop="totalQuantity">
								<el-input type="number" v-model="formData.totalQuantity" readonly
									placeholder="自动计算结果"></el-input>
							</el-form-item>
							<el-row :gutter="16">
								<el-col :span="12">
									<el-form-item label="未入库数量" prop="unstockedQuantity">
										<el-input v-model="formData.unstockedQuantity" readonly
											placeholder="自动计算结果"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="已入库数量" prop="stockedQuantity">
										<el-input v-model="formData.stockedQuantity" readonly
											placeholder="自动计算结果"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>
						<div>
							<div class="static-content-item">
								<div>账款&发票</div>
							</div>
							<el-form-item label="采购总额" prop="totalAmount">
								<el-input v-model="formData.totalAmount" readonly placeholder="自动计算结果"></el-input>
							</el-form-item>
							<el-row :gutter="16">
								<el-col :span="6">
									<el-form-item label="待付款金额" prop="pendingPaymentAmount">
										<el-input v-model="formData.pendingPaymentAmount" readonly
											placeholder="自动计算结果"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="6">
									<el-form-item label="已付款金额" prop="paidAmount">
										<el-input v-model="formData.paidAmount" readonly
											placeholder="自动计算结果"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="6">
									<el-form-item label="待回票金额" prop="pendingReceiptAmount">
										<el-input v-model="formData.pendingReceiptAmount" readonly
											placeholder="自动计算结果"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="6">
									<el-form-item label="已回票金额" prop="receivedAmount">
										<el-input v-model="formData.receivedAmount" readonly
											placeholder="自动计算结果"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>
						<!--  -->
						<el-form-item>
							<el-input type="text" ref="salesPerson" readonly clearable class="my1">
								<template #prefix>
									<div style="margin: 0;padding: 0;">
										<el-tabs v-model="activeName" @tab-click="handleClick" class="my">
											<el-tab-pane label="采购明细" name="order">
												<div class="flex-space-between"
													style="height: 48px;padding-left: 10px;">
													<el-text type="info" size="small">
														来自 <el-text type="primary" size="small">采购明细</el-text> 的数据
														共{{formData.tableData.length}}条
													</el-text>
													<el-text>
														<el-tooltip effect="light" :hide-after="50" content="创建"
															placement="top">
															<div class="my-a" @click="setTableData">
																<el-icon size="16px" color="#409EFF">
																	<CirclePlus />
																</el-icon>
															</div>
														</el-tooltip>
													</el-text>
												</div>
												<!--  -->
												<el-scrollbar style="width: calc(100vw - 48px - 32px);height: 300px;">
													<el-table @cell-dblclick="tableCellClick" v-loading="loading"
														:data="formData.tableData" :stripe="false" table-layout="auto"
														:default-sort="{}" border style="width: 100%;height: 300px;"
														class="my">
														<el-table-column width="76" fixed="left" prop="id"
															:sortable="false">
															<template #default="scope">
																<el-tooltip effect="light" :hide-after="50" content="复制"
																	placement="top">
																	<el-button style="padding: 0;" class="my"
																		@click="copyTableData(scope.$index)"
																		:icon="DocumentAdd"></el-button>
																</el-tooltip>
																<el-tooltip effect="light" :hide-after="50" content="删除"
																	placement="top">
																	<el-button style="padding: 0;" class="my"
																		@click="deleteTableData(scope.$index)"
																		:icon="Delete"></el-button>
																</el-tooltip>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="orderDetailId"
															label="采购需求*(可选)" :sortable="false">
															<template #default="scope">
																<div class="flex-align-center"
																	v-if="scope.row?.orderDetail">
																	<el-icon :color="scope.row?.orderDetail.color">
																		<component :is="scope.row?.orderDetail.icon">
																		</component>
																	</el-icon>
																	<el-text size="small" style="margin-left: 6px;">
																		{{scope.row?.orderDetail.label}}
																	</el-text>
																</div>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="pendingArrivalQuantity"
															label="待到货数量" :sortable="false">
															<template #default="scope">
																<el-text>{{scope.row.pendingArrivalQuantity}}</el-text>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="arrivedQuantity"
															label="已到货数量" :sortable="false">
															<template #default="scope">
																<el-text>{{scope.row.arrivedQuantity}}</el-text>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="returnedQuantity"
															label="已退货数量" :sortable="false">
															<template #default="scope">
																<el-text>{{scope.row.returnedQuantity}}</el-text>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="productId" label="采购产品"
															:sortable="false">
															<template #default="scope">
																<div class="flex-align-center">
																	<el-icon :color="scope.row.product?.color">
																		<component :is="scope.row.product?.icon">
																		</component>
																	</el-icon>
																	<el-text size="small"
																		style="margin-left: 6px;">{{scope.row.product?.label}}</el-text>
																</div>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="unitId" label="单位"
															:sortable="false">
															<template #default="scope">
																<el-text>{{scope.row.unitId}}</el-text>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="imageUrl" label="产品图片"
															:sortable="false">
															<template #default="scope">
																<el-image v-if="scope.row?.imageUrl"
																	style="width: 20px; height: 20px"
																	:src="scope.row.imageUrl" :zoom-rate="1.2"
																	:max-scale="7" :min-scale="0.2"
																	:preview-src-list="[scope.row.imageUrl]" fit="cover"
																	preview-teleported />
															</template>
														</el-table-column>
														<el-table-column width="140" prop="currentInventory"
															label="当前库存数量" :sortable="false">
															<template #default="scope">
																<el-text>{{scope.row.currentInventory}}</el-text>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="purchaseQuantity"
															label="采购数量*(可选)" :sortable="false">
															<template #default="scope">
																<el-text>{{scope.row.purchaseQuantity}}</el-text>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="purchasePrice"
															label="采购单价*(可选)" :sortable="false">
															<template #default="scope">
																<el-text>{{scope.row.purchasePrice}}</el-text>
															</template>
														</el-table-column>
														<el-table-column width="140" prop="totalPurchaseAmount"
															label="采购总额" :sortable="false">
															<template #default="scope">
																<el-text>{{scope.row.totalPurchaseAmount}}</el-text>
															</template>
														</el-table-column>
													</el-table>
												</el-scrollbar>
											</el-tab-pane>
										</el-tabs>
									</div>
								</template>
							</el-input>
						</el-form-item>
					</el-form>
				</el-scrollbar>
				<!--  -->
			</el-card>
			<!--  -->
			<levitation ref="levitationRef" :option="selectCell.option" :types="types" :st_type="selectCell.type"
				:params="levitationParams" :observerElement="scrollbarRef" :element="element" @change="levitationChange"
				@blur="levitationBlur" />
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
	import { reactive, ref } from 'vue';
	import { useRoute, useRouter } from 'vue-router';
	import request from '../../../utils/request';
	import { UploadFilled, ShoppingCart, UserFilled, WalletFilled, CirclePlus, DocumentAdd, Delete, Ship, CopyDocument } from '@element-plus/icons-vue';
	import { mergeObjects, formatNumber } from '../../../utils';
	import { useSelect } from '../../../utils/mixins';
	import { jwtDecode } from 'jwt-decode';
	import cookie from 'js-cookie';
	import levitation from '../../../components/levitation-form/levitation-form.vue';
	import { getShipments } from '../../../api/shipmentAPI';
	import { ElMessage, FormInstance } from 'element-plus';
	const emits = defineEmits(['update']);
	const { handSelectChange } = useSelect();
	const router = useRouter();
	const route = useRoute();
	const vFormRef = ref(null);
	const loading = ref(false);
	const activeName = ref('order');
	//levitation start
	const element = ref(null);
	const levitationRef = ref(null);
	const scrollbarRef = ref(null);
	const types = ref('');
	const selectCell = ref({
		id: 0,
		property: '',
		type: '',
		option: []
	});
	const levitationParams = ref(null);
	//levitation end

	// 表单
	const formData = ref({
		purchaseOrderId: null,
		procurementStatusId: null,
		procurementStatus: [],
		purchaseOrderNumber: null,
		supplierId: null,
		supplier: [],
		expectedArrivalDate: null,
		actualArrivalDate: null,
		abnormalStateId: null,
		remark: null,
		purchaseApplicant: null,
		purchaseApplicationDate: null,
		orderPassDate: null,
		totalQuantity: null,
		unstockedQuantity: null,
		stockedQuantity: null,
		totalAmount: null,
		pendingPaymentAmount: null,
		paidAmount: null,
		pendingReceiptAmount: null,
		receivedAmount: null,
		tableData: []
	});
	//验证
	const rules = reactive({
		procurementStatus: [{
			required: true,
			message: '采购状态不能为空',
			trigger: 'blur'
		}],
		purchaseOrderNumber: [],
		supplier: [{
			required: true,
			message: '供应商不能为空',
			trigger: 'blur'
		}],
		expectedArrivalDate: [{
			required: true,
			message: '预计到货日期不能为空',
			trigger: 'blur'
		}],
		actualArrivalDate: [{
			required: true,
			message: '实际到货日期不能为空',
			trigger: 'blur'
		}],
		abnormalStateId: [{
			required: true,
			message: '延期状态不能为空',
			trigger: 'blur'
		}],
		remark: [],
		purchaseApplicant: [{
			required: true,
			message: '采购申请人不能为空',
			trigger: 'blur'
		}],
		purchaseApplicationDate: [{
			required: true,
			message: '采购申请日期不能为空',
			trigger: 'blur'
		}],
		orderPassDate: [{
			required: true,
			message: '订单通过日期不能为空',
			trigger: 'blur'
		}]
	});
	const pageData = ref({
		pageNum: 1,
		pageSize: 1000,
		total: 0,
		pages: 0,
		list: []
	});
	//采购状态
	const procurementStatusOption = ref([]);
	const procurementStatusIndex = ref(-1);
	const procurementStatus = ref(null);
	//供应商
	const supplierOption = ref([]);
	const supplierIndex = ref(-1);
	const supplier = ref(null);
	//采购产品 table
	const productOption = ref([]);
	//订单明细 table
	const orderDetailOption = ref([]);
	const dialogVisible = ref(false);
	/**
	 * 打开弹出层
	 */
	const openDialog = () => {
		dialogVisible.value = true;
		setTimeout(async () => {
			const result = await getPageId();
			//
			if (result) {
				getprocurementStatus();
				getSupplierAll();
				//
				getOrderDetails();
				//如果是添加
				if (!route.query.id) {
					formData.value.procurementStatus = [148];
					formData.value.procurementStatusId = 148;
					procurementStatusIndex.value = 0;
				}
			}
		}, 10);
	}
	/**
	 * 关闭弹出层
	 */
	const closeDialog = () => {
		dialogVisible.value = false;
		vFormRef.value.resetFields();
		//清空url参数
		router.push({ query: {} });
		//
		formData.value.tableData.splice(0, formData.value.tableData.length);
	}
	//获取当前页面的数据
	const getPageId = async () => {
		if (!route.query.id) {
			const token = cookie.get('token');
			const decodedToken : {
				username : string
			} = jwtDecode(token);
			// console.log('decodedToken--->', decodedToken);
			//
			formData.value.purchaseApplicant = decodedToken?.username;
			//
			return true;
		}
		//
		const result = await request({
			url: '/demand/selectOrderById',
			params: {
				id: route.query.id
			},
		});
		//
		console.log('getPageId --->', result);
		//
		formData.value = { ...result.data.t.order };
		//
		formData.value.tableData = [...result.data.t.purchaseDetailList];
		formData.value.tableData.forEach((item : any, key : number) => {
			item.unitId = item.product.unitId;
			item.imageUrl = item.product.imageUrl;
			item.currentInventory = item.product.currentInventory;
			item.product = {
				label: `${item.product.productName}${item.product.model} | 在库${item.product.currentInventory}件`,
				value: item.product.productId,
				icon: UploadFilled,
				color: '#5283ff'
			}
		});
		console.log('tableData --->', formData.value.tableData);
		return true;
	}
	//设置一个
	const setTableData = () => {
		formData.value.tableData.push({
			id: +new Date(),
			productId: null,
			unitId: null,
			imageUrl: null,
			currentInventory: null,
			purchaseQuantity: null,
			costPrice: null,
			purchasePrice: null,
			totalPurchaseAmount: null,
			orderDetailId: null,
			orderDetail: null,//
			pendingArrivalQuantity: null,
			arrivedQuantity: null,
		});
	}
	//复制一个
	const copyTableData = (index : number) => {
		//...
		const item = { ...formData.value.tableData[index] };
		item.id = +new Date();
		formData.value.tableData.push(item);
	}
	//删除一个
	const deleteTableData = (index : number) => {
		formData.value.tableData.splice(index, 1);
	}
	const selectClear = (type : string) => {
		formData.value[type] = [];
	}
	const tableCellClick = (row : any, column : any, cell : HTMLTableCellElement, event : Event) => {
		types.value = '';
		//退货产品
		// if (column.property === 'productId') {
		// 	types.value = 'select-icon';
		// 	selectCell.value.option = [...productOption.value];
		// }
		//采购数量
		if (column.property === 'purchaseQuantity') {
			types.value = 'number';
		}
		//采购单价
		if (column.property === 'purchasePrice') {
			types.value = 'number';
		}
		//订单明细
		if (column.property === 'orderDetailId') {
			types.value = 'select-icon';
			selectCell.value.option = [...orderDetailOption.value];
		}
		element.value = cell;
		//
		selectCell.value.id = row.id;
		selectCell.value.property = column.property;
		//
		levitationParams.value = row[column.property];

		setTimeout(() => {
			levitationRef.value.getElementInfo();
		}, 1);
		//
	}
	//改变
	const selectChange = (value : Array<any>, type : string, key : number) => {
		//所有下拉框的配置列表
		const typeList = [{
			type: 'procurementStatus', //哪一个下拉框，根据ref
			option: procurementStatusOption, //下拉列表选项
			indexVar: procurementStatusIndex, //当前选中项
			idVar: 'procurementStatusId', //当前id值
			blurRef: procurementStatus, //ref
		}, {
			type: 'supplier', //哪一个下拉框，根据ref
			option: supplierOption, //下拉列表选项
			indexVar: supplierIndex, //当前选中项
			idVar: 'supplierId', //当前id值
			blurRef: supplier, //ref
		}];
		//
		handSelectChange(formData, typeList, value, type, key);
	}
	//获取采购状态
	const getprocurementStatus = async () => {
		//
		const data = await getShipments('procurement_status', 'option');
		console.log('getprocurementStatus --->', data);
		procurementStatusOption.value = [];
		data.forEach((item : any, key : number) => {
			procurementStatusOption.value.push(item);
			//
			if (formData.value.procurementStatusId === item.value) {
				procurementStatusIndex.value = key;
				formData.value.procurementStatus = [item.value];
			}
		});
	}
	//获取所有供应商
	const getSupplierAll = async () => {
		supplierOption.value = [];
		const result = await request({
			url: '/demand/supplierAll',
		});
		//
		console.log('getSupplierAll --->', result);
		//
		result.data.t.forEach((item : any, key : number) => {
			supplierOption.value.push({
				label: `${item.supplierShortName}`,
				value: item.supplierId,
				icon: Ship,
				color: '#ff5500'
			});
			if (formData.value.supplierId === item.supplierId) {
				formData.value.supplier = [item.supplierId];
				supplierIndex.value = key;
				//
			}
		});
		//
	}
	//获取所有采购需求(状态：已接受 143)
	const getOrderDetails = async () => {
		productOption.value = [];
		orderDetailOption.value = [];
		const result = await request({
			url: '/demand/selectDemandAll',
			params: { id: 143 }
		});
		//
		console.log('getOrderDetails --->', result);
		//
		result.data.t.list.forEach((item : any, key : number) => {
			orderDetailOption.value.push({
				label: `${item.product.productName}${item.product.model} | 在库${item.product.currentInventory}件`,
				value: item.demandId,
				icon: ShoppingCart,
				color: '#fa9e00',
				newId: item.product.productId,
				demandAmount: item.demandAmount
			});
		});
	}
	//根据id获取产品
	const getProductByPId = async (id : number | string, key : number, demandAmount : number) => {
		const result = await request({
			url: '/demand/selectProductByPId',
			params: {
				id
			}
		});
		//
		console.log('getProductByPId --->', result);
		formData.value.tableData[key] = mergeObjects(formData.value.tableData[key], result.data.t);
		formData.value.tableData[key].product = {
			label: `${result.data.t.productName}${result.data.t.model} | 在库${result.data.t.currentInventory}件`,
			value: result.data.t?.productId,
			icon: UploadFilled,
			color: '#5283ff'
		}
		//单位
		formData.value.tableData[key].unitId = result.data.t.unitId;
		//产品图片
		formData.value.tableData[key].imageUrl = result.data.t.imageUrl;
		//当前库存数量
		formData.value.tableData[key].currentInventory = result.data.t.currentInventory;
		//采购单价
		formData.value.tableData[key].purchasePrice = result.data.t.sellingPrice;
		//采购数量
		formData.value.tableData[key].purchaseQuantity = demandAmount;
		computeAll();
	}
	//根据id获取订单明细
	const getOrderDetailId = (id : number | string, key : number) => {
		if (!id) {
			return;
		}
		//mjg,zjx
		const result = orderDetailOption.value.filter(item => item.value === id)[0];
		console.log('getOrderDetailId --->', result);
		formData.value.tableData[key] = mergeObjects(formData.value.tableData[key], result);
		formData.value.tableData[key].orderDetail = result;
		//
		getProductByPId(result.newId, key, result.demandAmount);
	}
	//失去焦点 ***** 重要！重要！重要！
	const levitationBlur = () => {
		element.value = null;
	}
	//内容改变
	const levitationChange = (value : string | number) => {
		// console.log('value --->', value);
		formData.value.tableData.forEach((item : any, key : number) => {
			// console.log('item.id --->', item.id, 'selectCell.value.id --->', selectCell.value.id);
			if (item.id === selectCell.value.id) {
				item[selectCell.value.property] = value;
				//采购数量
				if (selectCell.value.property === 'purchaseQuantity') {
					computeAll();
				}
				if (selectCell.value.property === 'orderDetailId') {
					getOrderDetailId(value, key);
				}
			}
		});
	}
	//滚动
	const handleScroll = (e : any) => {
		// console.log('handleScroll --->', e);
		if (element.value === null) return;
		levitationRef.value.getElementInfo();
	}
	//提交表单
	const submitForm = async (formEl : FormInstance | undefined) => {
		if (!formEl) return;
		await formEl.validate(async (valid, fields) => {
			if (valid) {
				console.log('submitForm ---> ', formData.value);
				const data = { ...formData.value, purchaseDetails: [...formData.value.tableData] };
				delete data.procurementStatus;
				delete data.supplier;
				let type = '';
				if (!route.query.id) {
					type = '/addOrder';
				} else {
					type = '/updateOrderById';
				}
				//
				const result = await request({
					url: '/demand' + type,
					data: data,
					method: 'post'
				});
				//
				console.log('submitForm--->', result);
				//

				if (result.data.code === 200) {
					ElMessage.success({
						message: '操作成功！'
					});
					if (!route.query.id) {
						router.push({ query: { id: result.data.t } });
						route.query.id = result.data.t;
						//修改采购订单状态
						console.log('data.purchaseDetails --->', data.purchaseDetails);
						data.purchaseDetails.forEach((item : any, key : number) => {
							request({
								url: '/demand/selectDemandById',
								params: {
									demandId: item.orderDetailId
								},
							}).then((result1) => {
								const item1 = result1.data.t;
								if (item1.procurementDemandStatusId === 143) {
									item1.procurementDemandStatusId = 144;
									request({
										url: '/demand/updateDemand',
										method: 'post',
										data: item1,
									}).then((results : any) => {
										console.log('updateDemand --->', results);
									});
								}
							});
						});
					}
				} else {
					ElMessage.error({
						message: result.data.msg
					});
				}
				//
				setTimeout(() => {
					closeDialog();
					emits('update');
				}, 1);
			} else {
				//
				console.log('error submit!', fields);
			}
		});
	}
	//
	const computeAll = () => {
		formData.value.totalQuantity = 0;
		formData.value.unstockedQuantity = 0;
		formData.value.stockedQuantity = 0;
		formData.value.totalAmount = 0;
		formData.value.pendingPaymentAmount = 0;
		formData.value.paidAmount = 0;
		formData.value.pendingReceiptAmount = 0;
		formData.value.receivedAmount = 0;
		formData.value.tableData.forEach((item : any, key : number) => {
			//待到货数量
			item.pendingArrivalQuantity = 0 || (item.purchaseQuantity);
			//已到货数量
			item.arrivedQuantity = 0;
			//已退货数量
			item.returnedQuantity = 0;
			//采购总额
			item.totalPurchaseAmount = 0 || (item.purchaseQuantity * item.purchasePrice);
			//

			//采购总数
			formData.value.totalQuantity += Number(item.purchaseQuantity);
			//未入库数量
			formData.value.unstockedQuantity = 0;
			//已入库数量
			formData.value.stockedQuantity = 0;
			//采购总额
			formData.value.totalAmount += item.totalPurchaseAmount;
			//待付款金额
			formData.value.pendingPaymentAmount += item.totalPurchaseAmount;
			//已付款金额
			formData.value.paidAmount = 0;
			//待回票金额
			formData.value.pendingReceiptAmount = 0;
			//已回票金额
			formData.value.receivedAmount = 0;
		});
	}
	defineExpose({
		openDialog
	});
</script>
<style>
	@import '../../../assets/css/my-form-item.css';

	.form-item-bg {
		background-color: #bfdfff;
	}

	.form-item-bg1 {
		background-color: #a5f2e5;
	}

	.my.el-input .el-input__inner {
		opacity: 0;
	}

	/*  */
	.avatar-uploader {
		width: 178px;
		height: 178px;
	}

	.avatar-uploader .avatar {
		width: 100%;
		height: 100%;
	}

	.avatar-uploader .el-upload {
		width: 100%;
		height: 100%;
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}

	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}

	.el-icon.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		text-align: center;
	}

	.el-scrollbar__view {
		display: block !important;
	}

	/*  */
</style>

<style scoped lang="scss">
	@import '../../../assets/css/index.scss';
</style>